//
//  ViewController.m
//  UESTCPioneer
//
//  Created by Sway on 14-3-5.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "PioneerViewController.h"
#import "UPTableView.h"
#import "UPTitleCell.h"
#import "UPMainInfoCell.h"
#import "UPFooterCell.h"
#import "constant.h"
#import "LeveyTabBarController.h"
#import "PioneerNewsEntity.h"

@interface PioneerViewController (){
    
}
/*修改*/
@property (nonatomic,strong) NSMutableArray * collegeArray;
/**/
@end

@implementation PioneerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.leveyTabBarController.navigationItem setTitle:@"成电视角"];
    UnreadKey=kUnreadPioneerKey;
    
    [self hideTopView];
    tableViewTopConstraint.constant = 0;
    /*修改*/
    self.fromSourceEnable = YES;
    /**/
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    //请求初始化
    entityName=kPioneerEntityName;
    entityMapping=[Mapping PioneerMapping];
    requestData=@{@"type":@"getNews",@"page":@"1"};
    //请求评论列表用的参数
    commentListRequestData=@{@"type":@"getNewsComments",@"page":@"1",@"id":@"0"};
    //写评论请求用的参数
    commentWriteIdKey=@"id";
    commentWriteRequestData=@{@"type":@"setNewsComment",@"userName":[constant getUserName],@"id":@"0",@"comments":@""};
    commentIdKey=@"id";
    commentContentKey=@"comments";
    
    
    commentListKeyMapping=@{@"commentAuthor":@"userName",@"commentContent":@"commentBody",@"commentDate":@"commentDate"};
    
    /*修改*/
    [self getCollege];
    /**/
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.leveyTabBarController.navigationItem setTitle:@"成电视角"];
    /*修改*/
    self.isPioneer = YES;
    /**/
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ScreenTouch)];
    [self.tableView addGestureRecognizer:tapGesture];
    
//    /*添加20141026*/
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(235, 0, 110, 44)];
//    [btn setTitleEdgeInsets:UIEdgeInsetsMake(8, 0, 8, 25)];
//    [btn.titleLabel setTextAlignment:NSTextAlignmentLeft];
//    NSString *title = @"全部";
//    
//    if ([constant getCurrentView] == 2) {
//        title = @"最新";
//    }
//    
//    [btn setTitle:title forState:UIControlStateNormal];
//    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    [btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
//    btn.tintColor = [UIColor whiteColor];
//    [btn addTarget:self action:@selector(dropClicked:) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIImageView *xiala = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiala.png"]];
//    xiala.contentMode = UIViewContentModeScaleAspectFit;
//    xiala.frame = CGRectMake(88, 8+9, 12, 12);
//    xiala.tag = 22;
//    [btn addSubview:xiala];
//    
//    dropbtn = btn;
//    UIBarButtonItem *barItem2=[helper BarButtonItemWithUIButton:btn ButtonOrigin:CGPointMake(0, 0) CustomViewSize:CGSizeMake(110, 44)];
//    
//    self.leveyTabBarController.navigationItem.rightBarButtonItem = barItem2;
//    /**/
    [self _initBarButton];
}



#pragma  mark  触摸事件
-(BOOL)ScreenTouch{
    NSLog(@"screen tuch");
    if(dropDown == nil) {
        return NO;
    }
    else {
        [dropDown hideDropDown:dropbtn];
        [self rel];
        return YES;
    }
}

/*修改*/

-(void)getCollege{
    
    NSDictionary *getCollegeRequestData = @{@"type":@"getCollege"};
    
    [NetworkCenter AFRequestWithData:getCollegeRequestData SuccessBlock:^(AFHTTPRequestOperation *operation, id resultObject) {
        NSArray *dic = [NSJSONSerialization JSONObjectWithData:resultObject options:NSJSONReadingMutableLeaves error:nil];
        
        self.collegeArray = [NSMutableArray arrayWithArray:dic];
        
        //        NSLog(@"self.collegeArray = %@",self.collegeArray);
        //        NSLog(@"测试!");
    } FailureBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
        [Alert showAlert:@"获取学院列表错误"];
        return ;
    }];
}


- (void)dropClicked:(id)sender {
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    [arr addObject:@"全部"];
    for (NSDictionary * dic in  self.collegeArray) {
        [arr addObject:dic[@"partyName"]];
    }
    if(dropDown == nil) {
        CGFloat newHeight = 180;
        CGRect newfram = CGRectMake(215, -80, 105, 80);
        dropDown = [[NIDropDown alloc] initDropDown:sender withFram:newfram withHeight:newHeight content:arr forView:self.view];
        dropDown.delegate = self;
    }
    else {
        [dropDown hideDropDown:sender];
        [self rel];
    }
}



-(void)niDropDownDelegateMethod:(NIDropDown *)sender ForTitle:(NSString *)title ForIndex:(NSInteger)index{
    
    //    NSLog(@"下拉菜单的代理事件!");
    if ([title isEqualToString:@"全部"]){
        //        NSLog(@"全部");
        requestData = [RequestData AllNewsRequestData];
        [self.tableView beginRefreshing];
    }
    else{
        for (NSDictionary * dic in  self.collegeArray) {
            if ([title isEqualToString:dic[@"partyName"]]) {
                requestData = [RequestData CollegeRequestDataWithCode:dic[@"partyNo"]];
                [self.tableView beginRefreshing];
            }
        }
    }
    
    [self rel];
}
/*修改*/
/**/
//每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

//表的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [tableViewEntitiesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell*cell=[super tableView:tableView cellForRowAtIndexPath:indexPath];
    
    /*修改*/
    PioneerNewsEntity *entity = tableViewEntitiesArray[indexPath.section];
    self.source = entity.comeFrom;
    /**/
    if (indexPath.row == 0) {
        UPTitleCell *cell1 = (UPTitleCell*)cell;
        return cell1;
    }
    else if (indexPath.row == 1) {
        UPMainInfoCell*cell2=(UPMainInfoCell*)cell;
        return cell2;
    }
    else {
        UPFooterCell*cell3=(UPFooterCell*)cell;
        return cell3;
    }
    
}

//修改行高度的位置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    // indexPath.section,根据分组进行更精确的配置
    return 5;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
